<template>
  <div></div>
</template>

<script setup>
import { inject, onMounted, onBeforeUnmount } from 'vue'
import { onBeforeRouteUpdate, useRoute } from 'vue-router'
import { DrawLine, DrawRect, DrawPolygon, DrawCircle } from '@antv/l7-draw'
const { map, scene } = inject('mapScene')
let draw
onMounted(() => {
  let route = useRoute()
  const type = route.params.type
  initDraw(type)
})
//在切换路由的时候 需要重新给draw赋值
onBeforeRouteUpdate((to) => {
  const type = to.params.type
  initDraw(type)
})
function initDraw(type) {
  console.log(type)
  if (draw) {
    draw.destroy()
    draw = null
  }
  switch (type) {
    case 'line': {
      draw = new DrawLine(scene, {
        distanceOptions: {},
      })
      break
    }
    case 'rect': {
      draw = new DrawRect(scene, {
        distanceOptions: {},
      })
      break
    }
    case 'polygon': {
      draw = new DrawPolygon(scene, {
        distanceOptions: {},
      })
      break
    }
    case 'circle': {
      draw = new DrawCircle(scene, {
        distanceOptions: {},
      })
      break
    }
  }
  draw.enable()
}
onBeforeUnmount(() => {
  if (draw) {
    draw.destroy()
  }
})
</script>

<style lang="scss" scoped></style>
